import base64
from Crypto.Cipher import AES

# 补全长度为 16 的倍数
def pad(data):
    print(data)
    print(len(data) % 16)
    length = 16 - (len(data) % 16)
    print(length)
    print(data + (chr(length) * length).encode("utf-8"))
    return data + (chr(length) * length).encode()

# 去除补全的字符
def unpad(data):
    return data[:-data[-1]]

def encrypt(data):
    key = b'1234567812345678'
    cipher = AES.new(key, AES.MODE_ECB)
    padded_data = pad(data.encode())
    encrypted_data = cipher.encrypt(padded_data)

    print("encrypted_data,",encrypted_data)

    print("base64",base64.b64encode(encrypted_data))
    return base64.b64encode(encrypted_data).decode()

def decrypt(encrypted_data):
    key = b'1234567812345678'
    cipher = AES.new(key, AES.MODE_ECB)
    decoded_data = base64.b64decode(encrypted_data)
    decrypted_data = cipher.decrypt(decoded_data)
    return unpad(decrypted_data).decode()



username = 'felix'
password = '123456'

user_aes_encode = encrypt(username)
user_aes_decode = decrypt(user_aes_encode)

print("加密后:",user_aes_encode)

print("解密后:",user_aes_decode)